<template>
  <div class="user-profile-page">
    <div class="page-header">
      <div class="header-content">
        <el-icon><User /></el-icon>
        <h1>个人信息</h1>
        <p>查看和管理您的账户信息</p>
      </div>
    </div>

    <div class="profile-content">
      <el-row :gutter="24">
        <!-- 基本信息卡片 -->
        <el-col :span="16">
          <el-card class="info-card">
            <template #header>
              <div class="card-header">
                <span>基本信息</span>
              </div>
            </template>
            
            <div class="info-grid">
              <div class="info-item">
                <label>用户名：</label>
                <span>{{ userInfo.username || '-' }}</span>
              </div>
              <div class="info-item">
                <label>真实姓名：</label>
                <span>{{ userInfo.real_name || '-' }}</span>
              </div>
              <div class="info-item">
                <label>邮箱：</label>
                <span>{{ userInfo.email || '未设置' }}</span>
              </div>
              <div class="info-item">
                <label>手机号：</label>
                <span>{{ userInfo.phone || '未设置' }}</span>
              </div>
              <div class="info-item">
                <label>账户状态：</label>
                <el-tag :type="userInfo.status === 1 ? 'success' : 'danger'">
                  {{ userInfo.status === 1 ? '正常' : '禁用' }}
                </el-tag>
              </div>
              <div class="info-item">
                <label>创建时间：</label>
                <span>{{ formatDate(userInfo.create_time) }}</span>
              </div>
              <div class="info-item">
                <label>最后更新：</label>
                <span>{{ formatDate(userInfo.update_time) }}</span>
              </div>
            </div>
          </el-card>
        </el-col>

        <!-- 角色权限卡片 -->
        <el-col :span="8">
          <el-card class="roles-card">
            <template #header>
              <div class="card-header">
                <span>角色与权限</span>
              </div>
            </template>
            
            <div class="roles-section">
              <h3>分配角色</h3>
              <div v-if="roles.length > 0" class="roles-list">
                <el-tag 
                  v-for="role in roles" 
                  :key="role" 
                  type="primary" 
                  class="role-tag"
                >
                  {{ role }}
                </el-tag>
              </div>
              <div v-else class="no-roles">
                <el-empty description="暂无角色分配" :image-size="80" />
              </div>
            </div>

            <div class="permissions-section">
              <h3>系统权限</h3>
              <div v-if="permissions.length > 0" class="permissions-list">
                <el-tag 
                  v-for="perm in permissions.slice(0, 6)" 
                  :key="perm" 
                  type="success" 
                  class="perm-tag"
                >
                  {{ perm }}
                </el-tag>
                <div v-if="permissions.length > 6" class="more-tag">
                  <el-tag>+{{ permissions.length - 6 }} 更多</el-tag>
                </div>
              </div>
              <div v-else class="no-permissions">
                <el-empty description="暂无权限" :image-size="80" />
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { computed, onMounted } from 'vue'
import { User } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const userStore = useUserStore()

const userInfo = computed(() => userStore.user || {})
const roles = computed(() => userStore.roles || [])
const permissions = computed(() => userStore.permissions || [])

const formatDate = (dateStr) => {
  if (!dateStr) return '-'
  return new Date(dateStr).toLocaleString('zh-CN')
}

onMounted(() => {
  // 确保用户信息已加载
  if (!userStore.user) {
    userStore.fetchProfile()
  }
})
</script>

<style scoped lang="scss">
.user-profile-page {
  padding: var(--spacing-lg);
}

.page-header {
  margin-bottom: var(--spacing-xl);
  
  .header-content {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
    
    h1 {
      margin: 0;
      font-size: 24px;
      font-weight: 600;
    }
    
    p {
      margin: 0;
      color: var(--text-secondary);
    }
    
    .el-icon {
      font-size: 28px;
      color: var(--color-primary);
    }
  }
}

  .profile-content {
    height: 100%;
    min-height: 400px;
  }

  .el-row {
    height: 100%;
    align-items: stretch;
  }

  .el-col {
    display: flex;
    flex-direction: column;
  }

  .info-card, .roles-card {
    flex: 1;
    display: flex;
    flex-direction: column;
  }

  .info-card .el-card__body,
  .roles-card .el-card__body {
    flex: 1;
    display: flex;
    flex-direction: column;
  }

  .info-card, .roles-card {
    border-radius: var(--radius-lg);
    
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-weight: 600;
    }
  }

  .info-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: var(--spacing-md);
    
    .info-item {
      display: flex;
      align-items: center;
      padding: var(--spacing-sm) 0;
      
      label {
        font-weight: 500;
        color: var(--text-secondary);
        min-width: 100px;
      }
      
      span {
        color: var(--text-primary);
      }
    }
  }

  .roles-section, .permissions-section {
    margin-bottom: var(--spacing-xl);
    
    h3 {
      margin: 0 0 var(--spacing-md) 0;
      font-size: 16px;
      color: var(--text-secondary);
    }
  }

  .roles-list, .permissions-list {
    display: flex;
    flex-wrap: wrap;
    gap: var(--spacing-sm);
  }

  .role-tag, .perm-tag {
    margin-bottom: var(--spacing-xs);
  }

  .more-tag {
    margin-left: var(--spacing-sm);
  }

  .no-roles, .no-permissions {
    :deep(.el-empty) {
      padding: var(--spacing-lg) 0;
    }
  }

@media (max-width: 768px) {
  .info-grid {
    grid-template-columns: 1fr;
  }
  
  .profile-content {
    .el-col {
      width: 100%;
      
      &:nth-child(1) {
        margin-bottom: var(--spacing-lg);
      }
    }
  }
}
</style>